using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NBlock.MVCExtensions;
using Cost.Models;

namespace Cost.Controllers
{   
    public class SupplierController : Controller
    {
		#region 构造函数
		private readonly ICostRepository<Supplier> supplierRepository;

        public SupplierController(ICostRepository<Supplier> supplierRepository)
        {
			this.supplierRepository = supplierRepository;
        }
		#endregion 

        //
        // GET: /Supplier/

        public ViewResult Index()
        {
            return View();
        }

		//
        // GET: /Supplier/Edit/5
 
        public ActionResult Edit(string id)
        {
			var data = supplierRepository.Find(id) ?? new Supplier();
            return View(data);
        }

		//
        // POST: /Supplier/Save

        [HttpPost]
        public JsonResult Save(string id)
        {
			var data = supplierRepository.Find(id) ?? new Supplier();
			TryUpdateModel(data);		            
            supplierRepository.Save(data);
            return Json(nbAjaxMessage.Successful);
        }

		//
        // POST: /Supplier/Delete/5

        [HttpPost]
        public JsonResult Delete(string id)
        {
            supplierRepository.Delete(id);
            return Json(nbAjaxMessage.Successful);
        }

        [HttpPost]
        public JsonResult DeleteAll(string Ids)
        {
            var items = Ids.Split(',');
            supplierRepository.DeleteAll(d => items.Contains(d.Id));
            return Json(nbAjaxMessage.Successful);
        }

		//
        // POST: /Supplier/List

        [HttpPost]
        public JsonResult List(nbPageInfo pager, nbSearchCondition where)
        {
		    var pageData = supplierRepository.GetPageable(pager, where);
            var data = pager.ToGridData(pageData);
            return Json(data);
        }
    }
}

